package com.gitee.xmhzzz.component.spring.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * 指定名称为springContextUtil
 */
public class ApplicationContextUtil implements ApplicationContextAware, EnvironmentAware
{
    private static ApplicationContext context;

    private static Environment env;

    @Override
    public void setApplicationContext(ApplicationContext paramApplicationContext) throws BeansException
    {
        context = paramApplicationContext;
    }

    @Override
    public void setEnvironment(Environment environment) {
        env = environment;
    }

    public static Environment getEnvironment() {
        return env;
    }
    
    public static ApplicationContext getApplicationContext()
    {
        return context;
    }
    
    public static <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException
    {
        return context.getBeansOfType(type);
    }
    
    public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException
    {
        return context.getBeansWithAnnotation(annotationType);
    }
    
    public static Object getBean(String name) throws BeansException
    {
        return context.getBean(name);
    }
    
    public static boolean containsBean(String name)
    {
        return context.containsBean(name);
    }
    
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
    {
        return context.isSingleton(name);
    }
    
    public static Class getType(String name) throws NoSuchBeanDefinitionException
    {
        return context.getType(name);
    }

    public static <T> T getBean(Class<T> clazz)
    {
        return context.getBean(clazz);
    }

    public static <T> T getBean(String beanName, Class<T> clazz)
    {
        return context.getBean(beanName, clazz);
    }

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
    {
        return context.getAliases(name);
    }
}